<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
     <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 
      <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.1.0.js"></script>
 <link type="text/css" rel="stylesheet" href="${initParam.root }jquery.ratings.css" />
 <script src="${initParam.root }jquery-1.3.2.min.js"></script>
 <script src="${initParam.root }jquery.ratings.js"></script>
<script type="text/javascript">
	$(document).ready(function(){
		var itemNo = ${requestScope.ivo.itemNo};
		var starEval=0;
		
		$(":input[name=updateItem]").click(function(){
			if(confirm("수정하시겠습니까?")){
				location.href="${initParam.root}item/auth_updateItemContentPost.moja?itemNo="+itemNo;
			}else{
				return false;
			}
		});//update
		$(":input[name=deleteItem]").click(function(){
			if(confirm("삭제하시겠습니까?")){
				location.href="${initParam.root}item/delete.moja?itemNo="+itemNo;
			}else{
				return false;
			}
		});//delete
		$(":input[name=buyItem]").click(function(){
			var memberMoney = ${sessionScope.mvo.memberMoney};
			var itemPrice = ${requestScope.ivo.itemPrice};
	
			if(memberMoney >= itemPrice){
				if(confirm("구매하시겠습니까?")){
 					location.href="fileDownloadPoint.moja?itemNo=${requestScope.ivo.itemNo}&"+
					"memberId=${sessionScope.mvo.memberId}&sellerId=${requestScope.ivo.memberId}&fileDir=${requestScope.ivo.fileDir }";
				}else{
					alert("상품 구매가 취소되었습니다");
					return false;
				}
			}else{
				if(confirm("현재 보유 금액이 부족합니다. 충전하시겠습니까?")){
					
				}else{
					alert("상품 구매가 취소되었습니다");
					return false;
				}
			}
		});//buy
		$(":input[name=addCart]").click(function(){
			alert("${requestScope.ivo.fileDir }");
			if(confirm("장바구니에 담으시겠습니까?")){
				location.href="${initParam.root}item/addCart.moja?itemNo="+itemNo+"&memberId=${sessionScope.mvo.memberId}&fileDir=${requestScope.ivo.fileDir }";
			}else{
				return false;
			}
		});//buy
		
		$(":input[name=postItem]").click(function(){
			if($(":input[name=postTitle]").val()==""){
				alert("제목을 입력해주세요!");
				return false;
				
			}else if($(":input[name=postContent]").val()==""){
				alert("내용을 입력해주세요!");
				return false;
				
			}
			else if(confirm("상품후기를 게시하시겠습니까?")){				
				$.ajax({
			    	url:"${initParam.root }item/postResult.moja",
			        data:$("#postForm").serialize()+"&starEvaluation="+starEval,
			        dataType:"json", 
			        success:function(jsonData){
			        	$(":input[name=postTitle]").val("");
			        	$("#postContent").val("");
			        	avgCount=jsonData.count;
			        	var tl="<table border=1><tr bgcolor=lime><td>작성자</td><td>상품명</td><td width=500  align=center>내용</td><td>평점(평균값:"+jsonData.count+")</td><td></td></tr>";        	
			        	$.each(jsonData.list,function(index,post){
			        		//alert(post);
			        		var star="";
			        		if(post.starEvaluation==1){
			        			star="★";
			        		}else if(post.starEvaluation==2){
			        			star="★★";
			        		}else if(post.starEvaluation==3){
			        			star="★★★";
			        		}else if(post.starEvaluation==4){
			        			star="★★★★";
			        		}else{
			        			star="★★★★★";
			        		}
			        		tl+="<tr><td>"+post.memberId+"</td><td>"+post.itemName+"</td><td  align=center>"+post.postContent+"</td><td>"+star+"</td><td><a href=${initParam.root }item/postDelete.moja?memberId="+post.memberId+"&postContent="+post.postContent+"&itemNo="+post.itemNo+"><input type=button name=hukiDelete id=hukiDelete value=삭제></a></td></tr>";
			        	});
						tl+="</table>";
						$("#postView").html(tl);
			        }  	
				});
				/* location.href="${initParam.root }item/postResult.moja?itemNo="+itemNo+"&memberId=${requestScope.ivo.memberId}"; */
			}else{
				return false; 
			}
		});//postItem
			$(":button[name=postClose]").click(function(){
				$("#postView").html("");
			});
			$(":button[name=postGet]").click(function(){
				$.ajax({
			    	url:"${initParam.root }item/postGet.moja",
			        data:$("#postForm").serialize(),
			        dataType:"json", 
			        success:function(jsonData){
			        	var tl="<table border=1><tr bgcolor=lime><td>작성자</td><td>상품명</td><td width=500 align=center>내용</td><td>평점(평균값:"+jsonData.count+")</td><td></td></tr>";
			        	$.each(jsonData.list,function(index,post){
			        		var star="";
			        		if(post.starEvaluation==1){
			        			star="★";
			        		}else if(post.starEvaluation==2){
			        			star="★★";
			        		}else if(post.starEvaluation==3){
			        			star="★★★";
			        		}else if(post.starEvaluation==4){
			        			star="★★★★";
			        		}else{
			        			star="★★★★★";
			        		}
			        		
			        		//alert(post);
			        		tl+="<tr><td>"+post.memberId+"</td><td>"+post.itemName+"</td><td  align=center>"+post.postContent+"</td><td>"+star+"</td><td><a href=${initParam.root }item/postDelete.moja?memberId="+post.memberId+"&postContent="+post.postContent+"&itemNo="+post.itemNo+"><input type=button name=hukiDelete id=hukiDelete value=삭제></a></td></tr>";
			        	});
						tl+="</table>";
						$("#postView").html(tl);
			        }
				});
			});
			 $('#example-2').ratings(5).bind('ratingchanged', function(event, data) {
				 	starEval=data.rating;
				 
					$('#example-rating-2').text(data.rating);
			 });
			 
			 $(":input[name=messageBtn]").click(function(){
			
				 location.href="${initParam.root}item/messageInsertView.moja?memberId=${requestScope.ivo.memberId}&itemNo=${requestScope.ivo.itemNo}";
			 });
	});//ready
</script>
 <style>
.gray {
   color: #e9e9e9;
/*    border: solid 1px #555;
 */   background: #6e6e6e;
   background: -webkit-gradient(linear, left top, left bottom, from(#888), to(#575757));
   background: -moz-linear-gradient(top,  #888,  #575757);
   filter:  progid:DXImageTransform.Microsoft.gradient(startColorstr='#888888', endColorstr='#575757');
}
</style>
</head>
<body>
<div id="mainCss" align="center">
<table border="1">
<tr>
	<td colspan="4">
		<div align="center">
			<font size="5px">Item 게시글 확인<img src="${initParam.root }img/pen.jpg"></font>
		</div>
	</td>
</tr>
<tr>
	<td bgcolor="#D2D2D2" align="center"><font color="#000000" size="2px"><b>작성자</b></font></td>
	<td>${requestScope.ivo.memberId}<input type="button" name="messageBtn" value="쪽지쓰기"></td>
	<td bgcolor="#D2D2D2" align="center"><font color="#000000" size="2px"><b>상품번호</b></font></td>
	<td>${requestScope.ivo.itemNo}</td>
</tr>
<tr>
	<td bgcolor="#D2D2D2" align="center"><font color="#000000" size="2px"><b>상품명</b></font></td>
	<td>${requestScope.ivo.itemName }</td>
	<td bgcolor="#D2D2D2" align="center"><font color="#000000" size="2px"><b>상품가격</b></font></td>
	<td>${requestScope.ivo.itemPrice }<b>원</b></td>
</tr>
<tr>
	<td bgcolor="#D2D2D2" align="center"><font color="#000000" size="2px"><b>파일 url</b></font></td>
	<td colspan="4">${requestScope.ivo.fileDir }</td>
</tr>
<tr>
	<td bgcolor="#D2D2D2" align="center"><font color="#000000" size="2px"><b>업로드 일자</b></font></td>
	<td colspan="4">${requestScope.ivo.uploadDate }</td>
</tr>
<tr>
	<td colspan="4"><font color="#000000" size="2px"><b>상세설명</b></font><br>
	${requestScope.ivo.itemInfo }</td>
</tr>
<tr>
	<td colspan="4"><font color="#000000" size="2px"><b>카테고리(여러개 선택가능)</b></font></td>
</tr>

<tr>
 	<td colspan="4">
	
	
	<c:forEach items="${requestScope.ivo.cBean.keyList }" var="kList" varStatus="v">
	- ${kList } -
	 <c:forEach items="${requestScope.ivo.cBean.valList }" var="vList"  varStatus="k">
	 	
	 	<c:if test="${v.index == k.index }">
	 		
	 		<c:if test="${fn:contains(vList,kList) }">
	 		<%-- ${fn:indexOf(vList,'-')} --%>
	 		${fn:substringAfter(vList,'-')}
	 
	 	</c:if>
	 	</c:if> 
	 </c:forEach>

</c:forEach>


	 </td> 

</tr>


	 </td> 
</tr>

<tr>
<td colspan="4">
<c:choose>
	<c:when test="${sessionScope.mvo==null}">
		*로그인 후 구매 및 장바구니를 등록할 수 있습니다.
	</c:when>

	<c:when test="${sessionScope.mvo.memberId==requestScope.ivo.memberId}">
		<div align="center" id="seller">
		<input type="button" name = "updateItem" value="수정">
		<input type="button" name = "deleteItem" value="삭제">
		</div>
	</c:when>
	<c:when test="${sessionScope.mvo.memberPoint ==-1 }">
	<div align="center" id="admin">
			<input type="submit" value="이전페이지">
		</div>
	</c:when>
	 <c:otherwise>
		<div align="center" id="buyer">
		<input type="button" name = "buyItem" value="구매">
		<input type="button" name = "addCart" value="장바구니 추가">
		<input type="submit" value="이전페이지">
		</div>
	 </c:otherwise> 
</c:choose>
</td>
</tr>
</table>
<hr>
<form id="postForm"  action="${initParam.root }item/postResult.moja">
<input type="hidden" name="itemNo" value="${requestScope.ivo.itemNo}">
<input type="hidden" name="itemName" value="${requestScope.ivo.itemName}">

 <table border="1" >
    <tr>
    <td colspan="2">
    <div align="center">
    <font size="5px">상품 후기</font>
    </div>
    </td>
    </tr>
	 <tr >
		<td bgcolor="#D2D2D2" align="center"><font color="#000000" size="2px"><b>제목</b></font></td>
		<td><input type="text" name="postTitle" id="postTitle"></td>
	 </tr>
	 <tr>
		<td bgcolor="#D2D2D2" align="center"><font color="#000000" size="2px"><b>작성자</b></font></td>
		<td><input type="text" name="memberId" id="memberId" value="${sessionScope.mvo.memberId}" readonly="readonly"></td>
	 </tr>
	 <tr>
		<td colspan=2><textarea cols="50" rows="5" name="postContent" id="postContent"></textarea></td>
	 </tr>
	 <tr>
	 <td bgcolor="#D2D2D2" align="center"><font color="#000000" size="2px"><b>별 평점</b></font></td>
	 	<td align="center">
	 		<span id="example-2"></span>
	 		<!-- Your Rating: <input type="text" name="starEvaluation" readonly="readonly" id="example-rating-2"/> -->
    		 Your Rating: <span id="example-rating-2" >not set</span> 
	 	</td>
	 </tr>
	 <tr>
	 	<td  colspan=2 >
	 	<c:choose>

	<c:when test="${sessionScope.mvo.memberId==requestScope.ivo.memberId}">
		<div align="center">
	 		<input type="button" name="postGet" value="후기보기" class="gray">
	 		<input type="button" name="postClose" value="후기감추기" class="gray">
	 		</div>
	</c:when>

	 <c:otherwise>
		<div align="center">
	 		<input type="button" name="postItem" value="후기작성" class="gray">
	 		<input type="button" name="postGet" value="후기보기" class="gray">
	 		<input type="button" name="postClose" value="후기감추기" class="gray">
	 		</div>
	 </c:otherwise> 
</c:choose>
	 		
	 	</td>
	 </tr>
 </table>
 </form><br>
 <div id="postView"></div>
</div>
</body>
</html>